$(function () {
    xy_init();
});

function xy_init() {
    $("[name='xy_ssgs']").gsjgtree(1);
    $("[name='xy_gys']").qyjgzcombobox();
    $("[name='xy_kh']").qyjgzcombobox();

    var options = $.extend(getXyGridOption(0),
        {queryParams: {type: 0}});
    $('#xy-grid').datagrid(options);

//    setTimeout(function(){
//        $("#xy_search").click();
//    },500);
}

function xy_search() {
    var type = $("#xy-toolbar input[name='xy_type']").val();
    var xyhm = $("#xy-toolbar input[name='xy_xyhm']").val();
    var gys = $("#xy-toolbar input[name='xy_gys']").val();
    var ssgs = $("#xy-toolbar input[name='xy_ssgs']").val();
    var kh = $("#xy-toolbar input[name='xy_kh']").val();
    var spzt = $("#xy-toolbar input[name='xy_spzt']").val();
    var options = $.extend(getXyGridOption(type),
        {queryParams: {type: type, xyhm: xyhm, ssgsId: ssgs, gysId: gys, khId: kh, spzt: spzt}});
    $('#xy-grid').datagrid(options);
}

var actionhref = '';
//新增协议
function xy_addItem(title, action) {
    var href = $.webPath() + "/jhgl/xygl/xygl_content";
    actionhref = $.webPath() + "/jhgl/xygl/" + action;
    xy_showDialog(title, href, "");
}

function xy_editItem(title, action) {
    var record = $("#xy-grid").datagrid("getSelected");
    if (record == null) {
        $.messager.alert("提示", "请选择要修改的记录!", "info");
        return;
    }
    var href = $.webPath() + "/jhgl/xygl/xygl_content?xyId=" + record.id + "&type=" + record.type;
    actionhref = $.webPath() + "/jhgl/xygl/" + action;
    xy_showDialog(title, href, record.xyzt);
}

var xy_win;
//新增、修改企业法人弹出界面
function xy_showDialog(title, href, xyzt) {
    var button
    if (xyzt == '已保存')
        button = [
            {text: '保存', handler: xy_saveItem}
//            {text: '提交', handler: xy_commitItem}
        ];
    else
        button = [
            {text: '保存', handler: xy_saveItem}
        ];
    xy_win = $.window({
        title: title,
        iconCls: 'icon-add2',
        url: href,
        width: 750,
        height: 500,
        buttons: button,
        onBeforeClose: function () {
            window.parent.destroy('xy-uploadify');
        }
    });
}

function xy_saveItem() {
    if ($("#xy_lj_value").val())
        actionhref += '?xy.lj.id=' + $("#xy_lj_value").val();
    $("#xyglForm").form('submit', {
        url: actionhref,
        onSubmit: function () {
            return $(this).form('validate');
        },
        success: function (data) {
            var obj = eval('(' + data + ')');
            if (obj.statusCode == '200') {
                $(xy_win).window("close");
                $('#xy-grid').datagrid('reload');
                $('#xy_layout').layout('panel', 'center').panel('refresh');
                $.messager.show({title: '提示框', msg: obj.message, timeout: 3000, showType: 'slide'});
            } else {
                $.messager.alert('提示', obj.message, 'info');
            }
        }
    });
}

function xy_commitItem() {
    var row = $('#xy-grid').datagrid('getSelected');
    if (row) {
        $.messager.confirm('确认', '是否确认提交此协议', function (r) {
            if (r) {
                $.post($.webPath() + '/jhgl/xygl/commit_xy', {xyId: row.id}, function (data) {
                    var obj = eval('(' + data + ')');
                    if (obj.statusCode == '200') {
                        $('#xy-grid').datagrid('reload');
                        $('#xy_layout').layout('panel', 'center').panel('refresh');
//                    if ($("[name='xy.id']").val() != null)
//                        $(xy_win).window("close");
                        $.messager.show({title: '提示框', msg: obj.message, timeout: 3000, showType: 'slide'});
                    } else
                        $.messager.alert('提示', obj.message, 'info');
                });
            }
        });
    } else {
        $.messager.alert('提示', "请选择要提交的协议", 'info');
    }
}

function xy_destroyItem() {
    var row = $('#xy-grid').datagrid('getSelected');
    if (row) {
        $.messager.confirm('确认', '是否确认删除此协议', function (r) {
            if (r) {
                var index = $('#xy-grid').datagrid('getRowIndex', row);
                $.post($.webPath() + '/jhgl/xygl/delete_xy', {xyId: row.id}, function (data) {
                    var obj = eval('(' + data + ')');
                    if (obj.statusCode == '200')
                        $('#xy-grid').datagrid('deleteRow', index);
                    else
                        $.messager.alert('提示', obj.message, 'info');
                });
            }
        });
    } else {
        $.messager.alert('提示', "请选择要删除的协议", 'info');
    }
}

function xy_agreeItem() {
    var row = $('#xy-grid').datagrid('getSelected');
    if (row) {
        $.messager.confirm('确认', '是否确认履约此协议', function (r) {
            if (r) {
                var index = $('#xy-grid').datagrid('getRowIndex', row);
                $.post($.webPath() + '/jhgl/xygl/agree_xy', {xyId: row.id}, function (data) {
                    var obj = eval('(' + data + ')');
                    if (obj.statusCode == '200')
                        $('#xy-grid').datagrid('reload');
                    else
                        $.messager.alert('提示', obj.message, 'info');
                });
            }
        });
    } else {
        $.messager.alert('提示', "请选择要履约的协议", 'info');
    }
}

function xy_stopItem() {
    var row = $('#xy-grid').datagrid('getSelected');
    if (row) {
        $.messager.confirm('确认', '是否确认终止此协议', function (r) {
            if (r) {
                var index = $('#xy-grid').datagrid('getRowIndex', row);
                $.post($.webPath() + '/jhgl/xygl/stop_xy', {xyId: row.id}, function (data) {
                    var obj = eval('(' + data + ')');
                    if (obj.statusCode == '200')
                        $('#xy-grid').datagrid('reload');
                    else
                        $.messager.alert('提示', obj.message, 'info');
                });
            }
        });
    } else {
        $.messager.alert('提示', "请选择要终止的协议", 'info');
    }
}

function getXyGridOption(type) {
    var options = {
        url: $.webPath() + "/jhgl/xygl/xyList",
        toolbar: "#xy-toolbar",
        fitColumns: true,
        singleSelect: true,
        pageNumber: 1,
        pagination: true,
        fit: true,
        rownumbers: true,
        onLoadSuccess: function () {
            $("#xy-del").css('display', 'none');
            $("#xy-edit").css('display', 'none');
            $("#xy-commit").css('display', 'none');
            $("#xy-agree").css('display', 'none');
            $("#xy-stop").css('display', 'none');
            $(this).datagrid('selectRow', 0);

            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
        },
        loadFilter: function (data) {
            $.each(data.rows, function () {
                if (this.type) {
                    this.lx = this.type == "sfxy" ? "三方协议" : this.type == "cgxy" ? "采购协议" : "销售协议";
                    this.type = this.type == "sfxy" ? "3" : this.type == "cgxy" ? "1" : "2";
                    this.xyzt = this.xyzt == 'ysp' ? "已审批" : this.xyzt == 'ytj' ? "已提交" : this.xyzt == 'yly' ? "已完结" : this.xyzt == 'yzz' ? "已终止" : "已保存";
                }
                this.qzsj = this.kssj + "----" + this.jssj;
            })

            return data;
        },
        onSelect: function (rowIndex, rowData) {
            if (!rowData) {
                $("#xy-edit").css('display', 'none');
                $("#xy-del").css('display', 'none');
                $("#xy-agree").css('display', 'none');
                $("#xy-stop").css('display', 'none');
                $("#xy-commit").css('display', 'none');
                $('#xy_layout').layout('remove', 'center');
                return;
            }
            if (rowData.xyzt == '已保存') {
                $("#xy-edit").css('display', '');
                $("#xy-del").css('display', '');
                $("#xy-commit").css('display', '');
                $("#xy-agree").css('display', 'none');
                $("#xy-stop").css('display', 'none');
            } else if (rowData.xyzt == '已审批') {
                $("#xy-edit").css('display', 'none');
                $("#xy-del").css('display', 'none');
                $("#xy-agree").css('display', '');
                $("#xy-stop").css('display', '');
                $("#xy-commit").css('display', 'none');
            } else {
                $("#xy-edit").css('display', 'none');
                $("#xy-del").css('display', 'none');
                $("#xy-agree").css('display', 'none');
                $("#xy-stop").css('display', 'none');
                $("#xy-commit").css('display', 'none');
            }
        }, onDblClickCell: function (index, field) {
            if (field == "xyhm") {
                $("#xy-edit:visible").click();
            }
        }
    };
    if (type == 0) {
        var columns = {columns: [
            [
                {field: 'xyhm', title: '协议号', width: 60, align: 'center', formatter: xyCell},
                {field: 'lx', title: '协议类型', width: 20, align: 'center'},
                {field: 'qzsj', title: '起止时间', align: 'center', width: 50},
                {field: 'nxyl', title: '年协议量', align: 'right',
                    formatter: function (value) {
                        var sl = formatSlForHtml(value, ",");
                        return sl;
                    }, width: 30},
                {field: 'yxyl', title: '月协议量', align: 'right',
                    formatter: function (value) {
                        var sl = formatSlForHtml(value, ",");
                        return sl;
                    }, width: 30},
                {field: 'yzxl', title: '已执行量', align: 'right',
                    formatter: function (value) {
                        var sl = formatSlForHtml(value, ",");
                        return sl;
                    }, width: 30},
                {field: 'zdr', title: '制单人', align: 'center', width: 30},
                {field: 'xyzt', title: '审批', width: 20, align: 'center', formatter: formatXySp}
            ]
        ]}
        options = $.extend(options, columns);
    } else if (type == 1) {
        var columns = {columns: [
            [
                {field: 'xyhm', title: '协议号', align: 'center', width: 60},
                {field: 'lx', title: '协议类型', align: 'center', width: 20},
                {field: 'qzsj', title: '起止时间', align: 'center', width: 50},
                {field: 'gys_qc', title: '供应商', align: 'center', width: 40},
                {field: 'nxyl', title: '年协议量', formatter: function (value) {
                    var sl = formatSlForHtml(value, ",");
                    return sl;
                }, align: 'right', width: 30},
                {field: 'yxyl', title: '月协议量', align: 'right',
                    formatter: function (value) {
                        var sl = formatSlForHtml(value, ",");
                        return sl;
                    }, width: 30},
                {field: 'yzxl', title: '已执行量', align: 'right',
                    formatter: function (value) {
                        var sl = formatSlForHtml(value, ",");
                        return sl;
                    }, width: 30},
                {field: 'zdr', title: '制单人', align: 'center', width: 30},
                {field: 'xyzt', title: '审批', width: 20, align: 'center', formatter: formatXySp}
            ]
        ]}
        options = $.extend(options, columns);
    } else if (type == 2) {
        var columns = {columns: [
            [
                {field: 'xyhm', title: '协议号', align: 'center', width: 60},
                {field: 'lx', title: '协议类型', align: 'center', width: 20},
                {field: 'qzsj', title: '起止时间', align: 'center', width: 50},
                {field: 'kh_qc', title: '客户', align: 'center', width: 40},
                {field: 'nxyl', title: '年协议量', align: 'right',
                    formatter: function (value) {
                        var sl = formatSlForHtml(value, ",");
                        return sl;
                    }, width: 30},
                {field: 'yxyl', title: '月协议量', align: 'right',
                    formatter: function (value) {
                        var sl = formatSlForHtml(value, ",");
                        return sl;
                    }, width: 30},
                {field: 'yzxl', title: '已执行量', align: 'right',
                    formatter: function (value) {
                        var sl = formatSlForHtml(value, ",");
                        return sl;
                    }, width: 30},
                {field: 'zdr', title: '制单人', align: 'center', width: 30},
                {field: 'xyzt', title: '审批', width: 20, align: 'center', formatter: formatXySp}
            ]
        ]}
        options = $.extend(options, columns);
    } else {
        var columns = {columns: [
            [
                {field: 'xyhm', title: '协议号', align: 'center', width: 60},
                {field: 'lx', title: '协议类型', align: 'center', width: 20},
                {field: 'qzsj', title: '起止时间', align: 'center', width: 60},
                {field: 'gys_qc', title: '供应商', align: 'center', width: 40},
                {field: 'kh_qc', title: '客户', align: 'center', width: 40},
                {field: 'nxyl', title: '年协议量', align: 'right',
                    formatter: function (value) {
                        var sl = formatSlForHtml(value, ",");
                        return sl;
                    }, width: 20},
                {field: 'yxyl', title: '月协议量', align: 'right',
                    formatter: function (value) {
                        var sl = formatSlForHtml(value, ",");
                        return sl;
                    }, width: 20},
                {field: 'yzxl', title: '已执行量', align: 'right',
                    formatter: function (value) {
                        var sl = formatSlForHtml(value, ",");
                        return sl;
                    }, width: 20},
                {field: 'zdr', title: '制单人', align: 'center', width: 30},
                {field: 'xyzt', title: '审批', width: 20, align: 'center', formatter: formatXySp}
            ]
        ]}
        options = $.extend(options, columns);
    }
    return options;
}

function formatXySp(val, row) {
    if (!row.processInstanceId) {
        return val;
    } else {
        return '<a href="javascript:viewXyFlowPicture();" style="color: red">' + val + '</a>';
    }
}

function viewXyFlowPicture() {
    var row = $('#xy-grid').datagrid('getSelected');
    $.window({
        title: "流程历史",
        iconCls: 'icon-add2',
        url: $.webPath() + "/flow/publish/flowPicture?processInstanceId=" + encodeURI(encodeURI(row.processInstanceId)) + "&businessNo=" + row.xyhm,
        width: 650,
        height: 500,
        closed: true,
        modal: true,
        isMax: true
    });
}
